(function() {
	var ics = Iefgames_CS;
	if ($defined(ics.Comet)) {
		return ;
	}
	
	ics.Comet = function() {
		var port = 8080;
		var uri = ics.Core.baseUri + (port == 80 ? '' : ':' + port) + '/comet';

		var isComet = false;
		
		var getUsUuid = function() {
			var uuid = Cookie.read(ics.Cookie.uuidCookieName);
			if ($type(uuid) != 'string'
				|| uuid.length != 32) {
				Cookie.write(ics.Cookie.uuidCookieName, uuid = ics.Support.generateUuid(), ics.Cookie.Options);
			}
			
			return uuid;
		}
		
		var setCsUuid = function(csUuid) {
			Cookie.write(ics.Cookie.csUuidCookieName, csUuid, ics.Cookie.Options);
		}
		
		var createIframe = function() {
			var iframeId = 'cometIframe';
			var iframe = $(iframeId);
			if (!$defined(iframe) 
				|| $type(iframe) != 'element') {
				iframe = new IFrame({
					'id': iframeId,
					'src': 'http://im.cs.iefgames.com/iframe.html',
					'style': {
						'visibility': 'hidden'
					}
				});
				
			}
			
			return iframe;
		}
		
		var disposeIframe = function() {
			$('iframeId').dispose();
		}
		
		var start = function() {
			if (isComet) {
				return ;		//已经在comet连接了
			}
			
	
//			var comet = null;
//			if (Browser.Engine.trident) {		//IE 浏览器
//				cometByIE();
//			} else if (Browser.Engine.gecko) {		//firefox 浏览器
//				cometByW3c();
//			}
			
			cometByW3c();
		}
		
		var cometByIE = function() {
			try {
				var transferDoc = new ActiveXObject('htmlfile'); // !?!
				transferDoc.open();
				transferDoc.write('<html>');
				transferDoc.write('<script type="text/javascript"> document.domain="' + document.domain + '"; </script>');
				transferDoc.write('</html>');
				transferDoc.close();
				
				var div = transferDoc.createElement("div");
				div.innerHTML = '<iframe id="iframe" src="' +  uri 
					+ '?uuid=' + getUsUuid() 
					+ (Browser.Engine.trident ? '&browse=ie' : '') 
					+ (!!Cookie.read(ics.Cookie.csUuidCookieName) ? '&csUuid=' + Cookie.read(ics.Cookie.csUuidCookieName) : '')
					+ '&timestamp=' + new Date().getTime()
					+ '"></iframe>'
				transferDoc.body.appendChild(div);

				transferDoc._ = ics._;
			} catch(e) {
				var str = '';
				for (var key in e) {
					str += key + '=' + e[key] + '\n'
				}
				
				alert(str);
			}
		}
		
		var cometByW3c = function() {
			var iframe = createIframe().setProperty('src', uri 
					+ '?uuid=' + getUsUuid() 
					+ (Browser.Engine.trident ? '&browse=ie' : '') 
					+ (!!Cookie.read(ics.Cookie.csUuidCookieName) ? '&csUuid=' + Cookie.read(ics.Cookie.csUuidCookieName) : '')
					+ '&timestamp=' + new Date().getTime());
			$(document.body).grab(iframe);
		}
		
		var stop = function() {
			isComet = false;
			disposeIframe();
		}
		
		var restart = function() {
			stop();
			start();
		}
		
		var heartbeat = function() {
			alert(new Date());
		}
		
		var realStart = function(csUuid) {
			if (!csUuid
					|| csUuid.length != 32) {
				return;
			}
			
			setCsUuid(csUuid);
			
			isComet = true;
		}
		
		ics._$('stop', stop);
		ics._$('start', start);
		ics._$('comet', realStart);
		
		return {}
	}()	
})();